﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using System.Text;
using System.Collections;

namespace VSM
{
    //WIN-QG271FIL5FQ
    public partial class FollowUp : System.Web.UI.Page
    {
        
        private static GeneralFunctions GF = null;
        private static string dbType;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                          || Session[Utils.AppConstants.Password] == null
                          || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("../../Login.aspx");
            }

            if (!IsPostBack)
            {
                dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                
                LoadDefaultData();
                
                if (Request.QueryString["id"] != null && Request.QueryString["id"].Length > 0)
                {
                  
                }
              
            }
        }

        private void LoadDefaultData()
        {
            ddlDay.Items.Add("1");
            ddlDay.Items.Add("2");
            ddlDay.Items.Add("3");
            ddlDay.Items.Add("4");

            Services sv = new Services();
            string sql = "select '-1' as Code, 'All' as Name, '' as U_Position, '' as U_DealerCode ";
            sql += " union all ";
            sql += " select Code, Name, U_Position, U_DealerCode from [@TECHNICIAN] ";
            sql += " where U_DealerCode = '" + Session[Utils.AppConstants.ID_Dealer] + "'";

            DataSet dsAdviser = sv.ExecuteQuery("VSM", sql);
            ddlAdviser.DataSource = dsAdviser.Tables[0];
            ddlAdviser.DataTextField = "Name";
            ddlAdviser.DataValueField = "Code";
            ddlAdviser.DataBind();
            DataTable tbStatus = new DataTable();
            tbStatus.Columns.Add("StatusCode");
            tbStatus.Columns.Add("StatusName");
            tbStatus.Rows.Add(-1, "All");
            tbStatus.Rows.Add(0, "Pending");
            tbStatus.Rows.Add(1, "Called");

            ddlStatus.DataSource = tbStatus;
            ddlStatus.DataTextField = "StatusName";
            ddlStatus.DataValueField = "StatusCode";
            ddlStatus.DataBind();

            ddlStatus.Items.FindByValue("0").Selected = true;
        }
       
        protected void btn_Filter_Click(object sender, EventArgs e)
        {
            loadDataFilter();
        }

        private void loadDataFilter()
        {
            string selectedDate = ddlDay.SelectedItem.Text;

            string sql = " select ROW_NUMBER() OVER (ORDER BY JobID) AS RowNum, ";
            sql += " ServiceDate, gs.GeneralID, gs.JobID, Receptionist, jk.Name JobKindName, AdviserCode, tec.Name AdviserName, TechnicianCode, ";
            sql += " VIN, VehicleName, RegNo, CustomerName, Address, Tel, Mobile, ContactPerson, ContactPhone, isnull(FollowStatus, 0) FollowStatus, FollowRemarks ";
            sql += " from U_GeneralService gs ";
            sql += " join VSM_ORG..[@JOBKINDSVC] jk on jk.Code = gs.JobKind ";
            sql += "  join VSM_ORG..[@TECHNICIAN] tec on tec.Code = gs.AdviserCode ";
            sql += " where isnull(gs.WSCType, 'A') <> 'B' ";
            sql += " AND DATEDIFF(day, ServiceDate, GETDATE()) between 1 and " + selectedDate;
            sql += " AND gs.DealerCode = '" + Session[Utils.AppConstants.ID_Dealer] + "' ";
            if (ddlStatus.SelectedValue != "-1")
                sql += " and isnull(FollowStatus, 0) = " + ddlStatus.SelectedValue;

            if (ddlAdviser.SelectedValue != "-1")
                sql += " and gs.AdviserCode = " + ddlAdviser.SelectedValue;

            Services sv = new Services();
            DataTable dt = sv.ExecuteQuery(dbType, sql).Tables[0];

            grvPO.DataSource = dt;
            grvPO.DataBind();
        }



        protected void btn_Done_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.grvPO.Rows.Count; i++)
            {
                CheckBox checkBox = (CheckBox)this.grvPO.Rows[i].FindControl("chkDone");

                if (checkBox != null)
                {
                    if (checkBox.Checked)
                    {
                        Label gID = (Label)this.grvPO.Rows[i].FindControl("lbGeneralID");

                        Services sv = new Services();
                        sv.ExecuteQuery("DEALER", "UPDATE U_GeneralService SET IsReminded = 1 WHERE GeneralID = " + gID.Text);
                    }
                }
            }
            ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('Update Complete,');", true);
            loadDataFilter();
        }

        protected void btn_SaveClick(object sender, EventArgs e)
        {
           
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((Button)sender).Parent).Parent;
                Label lbl_GID = (Label)row.FindControl("lbGeneralID");
                TextBox txt_CustomerResponse = (TextBox)row.FindControl("txtCustomerRes");
                CheckBox chk_Done = (CheckBox)row.FindControl("chkDone");
               
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                dt.Columns.Add("FollowStatus");
                dt.Columns.Add("FollowRemarks");
                dt.Columns.Add("GeneralID");
                DataRow dr = dt.NewRow();
                dr["FollowStatus"] = chk_Done.Checked==true?1:0;
                dr["FollowRemarks"] = txt_CustomerResponse.Text;
                dr["GeneralID"] = lbl_GID.Text;
                dt.Rows.Add(dr);
                ds.Tables.Add(dt);
                Services sv = new Services();
                int count = sv.UpdateFollowUp("DEALER", ds);
                if (count > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('Operation complete');", true);
                    loadDataFilter();
                }
                else
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('Operation failed');", true);
                //Label lblNo = (Label)row.FindControl("lb_No");
                //int rowNo = Int32.Parse(lblNo.Text);
                //DataTable dtTmp = (DataTable)Session["dtDetail"];
                //Services sv = new Services();
                //foreach (DataRow dr in dtTmp.Rows)
                //{
                //    if (rowNo == Int32.Parse(dr["No"].ToString()))
                //    {
                //        string DetID = dr["DetailCode"].ToString();
                //        if (DetID != "-1")
                //        {
                //            DataSet dsDel = new DataSet();
                //            DataTable dtDel = new DataTable();
                //            dtDel.Columns.Add("DetailCode");
                //            DataRow drDel = dtDel.NewRow();
                //            drDel["DetailCode"] = DetID;

                //            dtDel.Rows.Add(drDel);
                //            dsDel.Tables.Add(dtDel);

                //            sv.DeleteGeneralServiceDetail("DEALER", dsDel);
                //        }
                //        dr.Delete();
                //        break;
                //    }
                //}
                //reset number
                //int index = 0;
                //foreach (DataRow dr in dtTmp.Rows)
                //    dr["No"] = ++index;

                //Session["dtDetail"] = dtTmp;
                //grvPO.DataSource = dtTmp;
                //grvPO.DataBind();

           
        }
    }
}